package com.luyu.rabbitmqdemo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * @author luYu
 */

@RestController
@RequestMapping("/mq")
@Slf4j
public class SendMessageController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @GetMapping("/confirm")
    public String sendConfirm(){
        rabbitTemplate.convertAndSend("amq.fanout","","你好啊");
        return "发送成功";
    }
    @GetMapping("/dir")
    public String sendDir(){
        rabbitTemplate.convertAndSend("luyu.dir","red","你好啊");
        return "发送成功";
    }
//    @GetMapping("/callback")
//    public String callback(){
//        CorrelationData correlationData=new CorrelationData(UUID.randomUUID().toString());
//        correlationData.getFuture().addCallback(res->{
//            if (res.isAck()){
//                System.out.println("消息发送成功:Id:"+correlationData.getId());
//            }else {
//                System.out.println("消息发送失败:Id:"+correlationData.getId()+"，失败原因:"+res.getReason());
//            }
//        },err->{
//            log.error("消息发送异常, ID:{}, 原因{}",correlationData.getId(),err.getMessage());
//        });
//        rabbitTemplate.convertAndSend("luyu.dir","call","你好啊",correlationData);
//        return "发送成功";
//    }
//    @GetMapping("/auto")
//    public String sendAuto(){
//        rabbitTemplate.convertAndSend("luyu.dir","auto","你好啊auto");
//        return "发送成功";
//    }
    @GetMapping("/ttl")
    public String sendTtl(){
        CorrelationData correlationData=new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("ttl.dir","ttl","你好ttl",correlationData);
        log.info("发生消息");
        return "发送成功";
    }
    @GetMapping("/lazy")
    public String sendLazy(){
        Message message= MessageBuilder
                .withPayload("lazyqueque".getBytes(StandardCharsets.UTF_8))
                .build();
        rabbitTemplate.convertAndSend("lazy.beanQueue",message);
        log.info("发生消息");
        return "发送成功";
    }


    @GetMapping("/test")
    public String test(){
        Message message= MessageBuilder
                .withPayload("test".getBytes(StandardCharsets.UTF_8))
                .build();

        rabbitTemplate.convertAndSend("test.direct","",message);
        return "发送成功";
    }

    @GetMapping("/ack")
    public String ackTest(){
        CorrelationData ackMessage = new CorrelationData();
        ackMessage.setId("9999999");
        ackMessage.getFuture().whenComplete((confirm, throwable) -> {
            if (confirm.isAck()){
                log.info("生成方收到消息发送成功");
            }
        });

        Message<String> message = MessageBuilder.withPayload("发送消息").build();
//        rabbitTemplate.convertAndSend("ack.exchange","red",message,ackMessage);
        rabbitTemplate.convertAndSend("queue.not.exist","red",message,ackMessage);
        return "发送成功";
    }

    @GetMapping("/return")
    public String returnTest(){
        Message<String> message = MessageBuilder.withPayload("发送消息").build();
        rabbitTemplate.convertAndSend("queue.not.exist","",message);
        return "发送成功";
    }
}
